package com.xl.qa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xl.basic.entity.Result;
import com.xl.basic.entity.ResultEnums;
import com.xl.basic.entity.UserInfo;
import com.xl.dao.domain.XlQuestionOrder;
import com.xl.qa.entity.param.AddIndexBannerParam;
import com.xl.qa.entity.param.QueryIndexBannerParam;
import com.xl.qa.entity.param.QueryQuesOrderParam;
import com.xl.qa.entity.param.UpdIndexBannerParam;
import com.xl.qa.service.IndexBannerService;
import com.xl.dao.domain.XlIndexBanner;
import com.xl.dao.mapper.XlIndexBannerMapper;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 钟勇
 * Create in 2024/2/4 15:20
 */
@Service
@RequiredArgsConstructor
public class IndexBannerServiceImpl implements IndexBannerService {

    private final XlIndexBannerMapper bannerMapper;

    @Override
    public Result<Integer> addIndexBanner(@NonNull AddIndexBannerParam param, UserInfo operator) {
        XlIndexBanner data = BeanUtil.copyProperties(param, XlIndexBanner.class);
        data.setState(1)
                .setAddTime(LocalDateTime.now())
                .setAddUser(operator == null ? null : operator.getUserId());
        if (bannerMapper.insert(data) == 0) {
            return ResultEnums.Exception.getResult("数据写入失败");
        }

        return ResultEnums.Success.getDataResult(data.getBannerId());
    }

    @Override
    public Result<Object> updIndexBanner(@NonNull UpdIndexBannerParam param, UserInfo operator) {
        XlIndexBanner updData = BeanUtil.copyProperties(param, XlIndexBanner.class);
        updData.setUpdTime(LocalDateTime.now())
                .setUpdUser(operator == null ? null : operator.getUserId());
        if (bannerMapper.updateById(updData) == 0) {
            return ResultEnums.Exception.getResult("数据写入失败");
        }

        return ResultEnums.Success.getResult();
    }

    @Override
    public Result<Object> delIndexBanner(@NonNull Integer bannerId, UserInfo operator) {
        if (bannerMapper.updateById(new XlIndexBanner()
                .setBannerId(bannerId)
                .setState(0)
                .setUpdTime(LocalDateTime.now())
                .setUpdUser(operator == null ? null : operator.getUserId())
        ) == 0) {
            return ResultEnums.Exception.getResult("数据删除失败");
        }

        return ResultEnums.Success.getResult();
    }

    @Override
    public Result<XlIndexBanner> getIndexBannerDetails(@NonNull Integer bannerId) {
        XlIndexBanner data = bannerMapper.selectById(bannerId);
        if (data.getState() != 1) {
            return ResultEnums.BusinessError.getResult("未找到对应广告");
        }
        return ResultEnums.Success.getDataResult(data);
    }

    @Override
    public Result<List<XlIndexBanner>> queryIndexBannerList(@NonNull QueryIndexBannerParam param) {
        List<XlIndexBanner> list = bannerMapper.selectList(getQueryQuesOrderWrapper(param));
        return ResultEnums.Success.getListResult(list);
    }

    private Wrapper<XlIndexBanner> getQueryQuesOrderWrapper(@NonNull QueryIndexBannerParam param) {
        return new LambdaQueryWrapper<XlIndexBanner>()
                .eq(XlIndexBanner::getState, 1)
                .like(StrUtil.isNotBlank(param.getTitleLike()), XlIndexBanner::getTitle, param.getTitleLike())
                .ge(param.getAddBeginTime() != null, XlIndexBanner::getAddTime, param.getAddBeginTime())
                .le(param.getAddEndTime() != null, XlIndexBanner::getAddTime, param.getAddEndTime())
                .orderByAsc(XlIndexBanner::getSortIndex)
                .orderByDesc(XlIndexBanner::getAddTime);
    }


}
